package cn.openea.perpartner.meily.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.meily.coupon.entity.Coupon;
import cn.openea.perpartner.meily.coupon.mapper.CouponMapper;
import cn.openea.perpartner.meily.coupon.service.ICouponService;
import cn.openea.perpartner.meily.thirdparty.payment.CouponSystemApi;
import cn.openea.perpartner.meily.web.entity.Goods;
import cn.openea.perpartner.meily.web.service.IGoodsService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author rains
 * @since 2023-06-16 11:35
 */
@Slf4j
@Service("couponService")
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>  implements ICouponService {

    @Resource
    private CouponSystemApi couponAmountApi;
    @Resource
    private IGoodsService goodsService;

    /**
     * 发放优惠券
     * @param coupon
     * @return
     */
    @Override
    public boolean issueCoupons(Coupon coupon) {

        String couponName = "平台优惠券";// 优惠券名称
        String batchId = "";// 批次ID
        String useScope = "";// 适用范围

        if (coupon.getCouponAmount().compareTo(BigDecimal.ZERO) > 0) {
            // 优惠券类型。0单品券；1通用券
            if ("0".equals(coupon.getCouponType())) {

                // 获取商品信息
                if(StrUtil.isNotBlank(coupon.getGoodsNo())){
                    LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(Goods::getGoodsNo, coupon.getGoodsNo());
                    Goods goods = goodsService.getOne(wrapper);
                    if(BeanUtil.isEmpty(goods)){
                        throw new ServiceException("根据商品编号("+coupon.getGoodsNo()+")找不到此商品！");
                    }
                    couponName = goods.getGoodsName() + "专属优惠券";
                    batchId = String.valueOf(goods.getGoodsId());
                    useScope = String.valueOf(goods.getGoodsId());
                } else {
                    throw new ServiceException("请填写商品编号！");
                }
            }

            JSONObject object = new JSONObject();
            object.put("userId", coupon.getUserId());// 用户id
            object.put("couponName", couponName);// 优惠券名称
            object.put("couponAmount", coupon.getCouponAmount());// 优惠券金额
            if (StrUtil.isNotBlank(batchId)) {
                object.put("batchId", batchId);// 批次ID
            }
            if (StrUtil.isNotBlank(useScope)) {
                object.put("useScope", useScope);// 适用范围
            }

            JSONObject resObj = couponAmountApi.sendCoupon(object);
            if (resObj.isEmpty() || !"200".equals(resObj.getString("code"))) {
                throw new ServiceException("发放优惠券失败！");
            }
        }

        return true;
    }

}
